package com.harunasan.carrental.common.query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.harunasan.carrental.common.exception.OperationException;
import com.harunasan.carrental.common.utils.CamelAndUnderLineConverterUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 * <p>
 * 公共查询类
 * </p>
 *
 * @author 梁毅
 * @since 2022/2/27
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CommonQueryParam extends ListQueryParam {

    @ApiModelProperty("参数json串")
    private List<QueryParam> queryParams;

    @ApiModelProperty("是否正序")
    private Boolean isAsc;

    @ApiModelProperty("排序名称")
    private String sortColumns;

    @Override
    public String getSortColumns() {
        return CamelAndUnderLineConverterUtil.getColumnName(sortColumns);
    }

    /**
     * 参数json串
     *
     * @param queryParams 参数json串
     */
    public void setQueryParams(String queryParams) {
        try {
            this.queryParams = JSON.parseObject(queryParams, new TypeReference<List<QueryParam>>() {
            });
        } catch (JSONException jsonException) {
            throw new OperationException("查询参数异常:" + queryParams);
        }
    }

    /**
     * <p>
     * 公共查询类-参数json串实体
     * </p>
     *
     * @author 梁毅
     * @since 2021/12/21
     */
    @Data
    public static class QueryParam {
        @ApiModelProperty("查询字段名称")
        private String columnName;

        @ApiModelProperty("查询条件")
        private String selectCondition;

        @ApiModelProperty("查询值")
        private List<String> val;

        public String getColumnName() {
            return CamelAndUnderLineConverterUtil.getColumnName(columnName);
        }
    }
}
